﻿using JinJun.YShow.BLL.IService;
using JinJun.YShow.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace JinJun.YShow.BLL.Service
{
    public class ProductService : BaseService<Product_Spu>, IProductService
    {
        public bool DeleteProduct(Product_Spu product_Spu, Pic pic)
        {
            //创建实例
            using (var db = new YShowDBEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Entry(product_Spu).State = EntityState.Deleted;

                        db.Entry(pic).State = EntityState.Deleted;
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 添加两表--商品表和商品spu规格关联表
        /// </summary>
        /// <param name="product_Spu"></param>
        /// <param name="specs"></param>
        /// <returns></returns>
        public bool BatchAdd(Product_Spu product_Spu, List<Spec> specs)
        {
            //创建实例
            using (var db = new YShowDBEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //添加商品表
                        db.Product_Spu.Add(product_Spu);
                        //保存
                        db.SaveChanges();

                        //循环添加商品spu规格关联表
                        for (int i = 0; i < specs.Count; i++)
                        {
                            Produc_Spu_Spec produc_Spu_Spec = new Produc_Spu_Spec();
                            produc_Spu_Spec.CreateTime = DateTime.Now;
                            produc_Spu_Spec.ModifiedTime = DateTime.Now;
                            produc_Spu_Spec.Spec_Id = specs[i].Id;
                            produc_Spu_Spec.Produc_Spu_Id = product_Spu.Id;
                            db.Produc_Spu_Spec.Add(produc_Spu_Spec);
                        }

                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 添加两表--商品sku表和商品sku规格值关联表
        /// </summary>
        /// <param name="product_Spu"></param>
        /// <param name="specs"></param>
        /// <returns></returns>
        public bool EditStock(Product_Sku product_Sku, Produc_Sku produc_Sku)
        {
            //创建实例
            using (var db = new YShowDBEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //添加商品sku表
                        db.Product_Sku.Add(product_Sku);
                        //保存
                        db.SaveChanges();

                        //添加商品sku规格值关联表
                        produc_Sku.Product_Sku_Id = product_Sku.Id;
                        db.Produc_Sku.Add(produc_Sku);

                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 添加两表--商品表和商品spu规格关联表
        /// </summary>
        /// <param name="product_Spu"></param>
        /// <param name="specs"></param>
        /// <returns></returns>
        public bool BatchEdit(Product_Spu product_Spu, List<Spec> specs, List<Produc_Spu_Spec> produc_Spu_Specs)
        {
            //创建实例
            using (var db = new YShowDBEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //修改商品表
                        db.Entry(product_Spu).State = EntityState.Modified;

                        //删除
                        for (int i = 0; i < produc_Spu_Specs.Count; i++)
                        {
                            var entity = db.Produc_Spu_Spec.Find(produc_Spu_Specs[i].Id);
                            if (entity != null)
                            {
                                db.Produc_Spu_Spec.Remove(entity);
                            }
                        }

                        //循环添加商品spu规格关联表
                        for (int i = 0; i < specs.Count; i++)
                        {
                            Produc_Spu_Spec produc_Spu_Spec = new Produc_Spu_Spec();
                            produc_Spu_Spec.CreateTime = DateTime.Now;
                            produc_Spu_Spec.ModifiedTime = DateTime.Now;
                            produc_Spu_Spec.Spec_Id = specs[i].Id;
                            produc_Spu_Spec.Produc_Spu_Id = product_Spu.Id;
                            db.Produc_Spu_Spec.Add(produc_Spu_Spec);
                        }

                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 添加两表--商品表和商品spu规格关联表
        /// </summary>
        /// <param name="product_Spu"></param>
        /// <param name="specs"></param>
        /// <returns></returns>
        public bool BatchEdit(Product_Spu product_Spu, List<Spec> specs, List<Produc_Spu_Spec> produc_Spu_Specs, List<Product_Sku> product_Skus, List<Produc_Sku> produc_Skus)
        {
            //创建实例
            using (var db = new YShowDBEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //修改商品表
                        db.Entry(product_Spu).State = EntityState.Modified;

                        //删除
                        for (int i = 0; i < produc_Spu_Specs.Count; i++)
                        {
                            var entity = db.Produc_Spu_Spec.Find(produc_Spu_Specs[i].Id);
                            if (entity != null)
                            {
                                db.Produc_Spu_Spec.Remove(entity);
                            }
                        }

                        //循环添加商品spu规格关联表
                        for (int i = 0; i < specs.Count; i++)
                        {
                            Produc_Spu_Spec produc_Spu_Spec = new Produc_Spu_Spec();
                            produc_Spu_Spec.CreateTime = DateTime.Now;
                            produc_Spu_Spec.ModifiedTime = DateTime.Now;
                            produc_Spu_Spec.Spec_Id = specs[i].Id;
                            produc_Spu_Spec.Produc_Spu_Id = product_Spu.Id;
                            db.Produc_Spu_Spec.Add(produc_Spu_Spec);
                        }

                        //删除
                        for (int i = 0; i < product_Skus.Count; i++)
                        {
                            var entity = db.Product_Sku.Find(product_Skus[i].Id);
                            if (entity != null)
                            {
                                db.Product_Sku.Remove(entity);
                            }
                        }

                        //删除
                        for (int i = 0; i < produc_Skus.Count; i++)
                        {
                            var entity = db.Produc_Sku.Find(produc_Skus[i].Id);
                            if (entity != null)
                            {
                                db.Produc_Sku.Remove(entity);
                            }
                        }

                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }
    }
}
